// components/seach_bar/search_bar.js
Component({
  /**
   * 组件的属性列表
   */
  properties: {
    hide_btn:{
      type:"Boolean",
      value:false
    },
    icon_size:{
      type:"Number",
      value:20
    },
    icon:{
      type:"String",
      value:""
    },
    s_list:{
      type:"Array",
      value: []
    }
  },

  /**
   * 组件的初始数据
   */
  data: {
    s_word: "",
    l_hide:!0,
  },
  /**
   * 进入
   */
  created(){
    this.preventShack = this.preventShack();
  },
  /**
   * 组件的方法列表
   */
  methods: {
    //按钮
    search:function(e){
      // console.log("tap");
      let mySearchData = {
        s_word: this.data.s_word
      },//detail对象，提供给事件监听函数
      myListOption = {};//事件触发选项
      this.triggerEvent("mysearch", mySearchData, myListOption);
    },
    //输入
    matchWord:function(e){
      let t = e.detail.value;
      t = t ? t.replace(/\s+/g, "") : "";
      this.preventShack();
      if(t){
        this.setData({
          l_hide:!1,
          s_word: t
        })
      }else{
        this.setData({
          l_hide: !0,
          s_word:""
        })
      }
    },
    //防抖动
    preventShack:function(){
      let timer=null;
      return function(){
        console.log(timer);
        if(timer)clearTimeout(timer)
        timer=setTimeout(()=>{
          console.log("timeout",timer);
          timer=null;
          let mySearchData = {
            s_word: this.data.s_word
          }
          this.triggerEvent("mysearch", mySearchData);
        },2000);
      }
    },
    //点击列表
    getItem:function(e){
      let i = e.currentTarget.dataset.index, li = this.properties.s_list,c=li[i];
      let myClickData={index:i,cur:c}
      this.triggerEvent("myselect",myClickData);
    },
  }
})
